package hlhj.fhp.supreme.activitys

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import com.bumptech.glide.Glide
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import fhp.hlhj.giantfold.utils.LogUtil
import hlhj.fhp.supreme.BaseAty
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.javabean.ClassDetailBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import hlhj.fhp.supreme.utils.MyUtils
import kotlinx.android.synthetic.main.activity_class_detail_aty.*
import kotlinx.android.synthetic.main.confirm_item.*

class ClassDetailAty : BaseAty() {
    private var idStr=""
    private var idStrs=""
    private var totalMoney=0.0
    private var money=0.0
    private val PAY=1
    private var lid="1"
    private var preId=0
    private var nextId=0
    private var pMoney=""
    private var nMoney=""
    private var id=0
    override fun getContentId(): Int {
        return R.layout.activity_class_detail_aty
    }
    override fun initView() {
        id=intent?.getIntExtra("id",0)!!
        OkGo.post<ClassDetailBean>(Urls.BaseUrl+Urls.CLASS_DETAIL)
                .params("id",intent?.getIntExtra("id",0)!!)
                .execute(object :JsonCallBack<ClassDetailBean>(this){
                    override fun onSuccess(response: Response<ClassDetailBean>?) {
                        val bean = response?.body()
                        if (bean?.code==1){
//                            MyUtils.toast("---${bean?.data.is_auth}")
                            if (bean?.data.is_auth==1){
                                val i = Intent(this@ClassDetailAty, ClassVedioAty::class.java)
                                i.putExtra("id",intent?.getIntExtra("id",0).toString())
                                startActivity(i)
                                finish()
                            }else{
                                lo.visibility=View.VISIBLE
                                userName.text=bean?.data.teacher_name
                                userInfo.text=bean?.data.teacher_des
                                Glide.with(this@ClassDetailAty).load(bean?.data.teacher_img).into(userIcon)
                                className.text=bean?.data.title
                                classContent.text=bean?.data.desc
                                tvPrice.text="￥${bean?.data.money}"
                                tv3.text="购买全部${bean?.data.level_name}课程"
                                idStr=bean?.data.id.toString()
                                idStrs=bean?.data.id.toString()

                                preId=bean?.data.prev.id
                                nextId=bean?.data.next.id
                                if (preId==0){
                                    btPre.visibility=View.GONE
                                    tvM.visibility=View.GONE
                                }else{
                                    btPre.visibility=View.VISIBLE
                                }
                                if (nextId==0){
                                    btNext.visibility=View.GONE
                                    tvM.visibility=View.GONE
                                }else{
                                    btNext.visibility=View.VISIBLE
                                }
                                totalMoney=bean?.data.level_price.toDouble()
                                money=bean?.data.money.toDouble()
                                /*for (i in 0..bean?.data.level_brother.size-1){
                                    LogUtil.log("${bean?.data.level_brother[i].money.toDouble()}")
                                    LogUtil.log("${bean?.data.level_brother[i].money}")
                                    idStrs=idStrs+","+bean?.data.level_brother[i].id
                                    totalMoney=totalMoney+bean?.data.level_brother[i].money.toDouble()
                                }*/
                            }
                        }
                    }
                })
    }

    override fun initListener() {
        btExit.setOnClickListener {
            finish()
        }
        btCheck.setOnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked){
                tvPrice.text="￥"+MyUtils.formatDouble(totalMoney.toDouble(),2)
            }else{
                tvPrice.text="￥"+MyUtils.formatDouble(money.toDouble(),2)
            }
        }

        btBuy.setOnClickListener {
            val intent = Intent(this, PayAty::class.java)
            intent.putExtra("et",2)
            intent.putExtra("type","2")
            intent.putExtra("ids",if (btCheck.isChecked)idStrs else idStr)
            intent.putExtra("money",(if (btCheck.isChecked)totalMoney else money).toString())
            startActivityForResult(intent,PAY)
        }

        btPre.setOnClickListener {
            val intent = Intent(this, ClassDetailAty::class.java)
            intent.putExtra("id",preId)
            intent.putExtra("lid","")
            startActivity(intent)
            finish()
        }
        btNext.setOnClickListener {
            val intent = Intent(this, ClassDetailAty::class.java)
            intent.putExtra("id",nextId)
            intent.putExtra("lid","")
            startActivity(intent)
            finish()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode!=-1)return
        if (requestCode==PAY){
            btBuy.setText("去观看")
            btBuy.setOnClickListener {
                val i = Intent(this@ClassDetailAty, ClassVedioAty::class.java)
                i.putExtra("id",intent?.getIntExtra("id",0).toString())
                startActivity(i)
                finish()
            }
//            recreate()
        }
    }
}
